exports.__esModule = true;

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

exports.default = class extends think.Service {
    constructor() {
        super();
        this.MDictModel = this.db['sys_mdict'];
    }

    /**
     * 根据搜索条件查询字典列表
     */
    queryDictBySeach(data) {
        var _this = this;

        return _asyncToGenerator(function* () {
            let conditionStr = '';
            if (data.status) {
                conditionStr += ` and sys_mdict.status=${data.status}`;
            } else {
                conditionStr += ` and sys_mdict.status!=1`;
            }
            if (data.tree_name) {
                conditionStr += ` and sys_mdict.tree_name like '%${data.tree_name}%'`;
            }
            let sql = `select sys_mdict.*,DATE_FORMAT(sys_mdict.create_date,'%Y-%m-%d %h:%i:%s') create_date,
                    DATE_FORMAT(sys_mdict.update_date,'%Y-%m-%d %h:%i:%s') update_date,create_user.realname as create_realname,
                    update_user.realname as update_realname from sys_mdict 
                   left join sys_user create_user on create_user.id=sys_mdict.create_by
                   left join sys_user update_user on update_user.id=sys_mdict.update_by where 1=1 ${conditionStr}`;
            let list = yield _this.CrudService.querySql(sql);
            if (list.length > 0) {
                return yield new _this.TreeService().transData(list);
            } else {
                return list;
            }
        })();
    }

    /**
     * 查询字典列表
     */
    queryDictList(data) {
        var _this2 = this;

        return _asyncToGenerator(function* () {
            let conditionStr = '';

            let list = yield _this2.MDictModel.findAll();
            return list;
        })();
    }

    /**
     * 保存多级字典
     */
    saveMDict(data) {
        var _this3 = this;

        return _asyncToGenerator(function* () {
            try {
                let parent = yield _this3.MDictModel.findOne({ where: { id: data.parent_id }, row: true });
                data.parent_ids = '';
                data.tree_names = '';
                data.level = '';
                data.create_date = new Date();
                data.update_date = new Date();
                if (parent === null) {
                    parent = {
                        id: 0,
                        parent_ids: '',
                        tree_names: '',
                        level: 0,
                        tree_name: '',
                        sort: 0,
                        parent_id: ''
                    };
                }
                return yield new _this3.TreeService().saveTree('sys_mdict', parent, data);
            } catch (error) {
                return _this3.exceptionService.handleError(error);
            }
        })();
    }

    /**
     * 修改多级字典
     */
    updateMDict(data) {
        var _this4 = this;

        return _asyncToGenerator(function* () {
            try {
                let parent = yield _this4.MDictModel.findOne({ where: { id: data.parent_id }, row: true });
                data.parent_ids = '';
                data.tree_names = '';
                data.level = '';
                data.create_date = new Date();
                data.update_date = new Date();
                return new _this4.TreeService().saveTree('sys_mdict', parent, data);
            } catch (error) {
                return _this4.exceptionService.handleError(error);
            }
        })();
    }

    /**
     * 根据id删除多级字典及其所有字节点，
     */
    deleteMDict(id) {
        var _this5 = this;

        return _asyncToGenerator(function* () {
            try {
                const Op = _this5.db.sequelize.Op;
                return new _this5.TreeService().deleteParentAndChildrenNode('sys_mdict', id);
            } catch (error) {
                return _this5.exceptionService.handleError(error);
            }
        })();
    }

    /**
     * 根据id删除多级字典及其所有字节点，
     */
    updateDictStatus(id, status) {
        var _this6 = this;

        return _asyncToGenerator(function* () {
            try {

                return _this6.MDictModel.update({ status: status }, { where: { id: id } });
            } catch (error) {
                return _this6.exceptionService.handleError(error);
            }
        })();
    }
};